﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using Hxsoft.AppV1.Module;

namespace Hxsoft.AppV1.WebUI
{
	/// <summary>
	/// 评论列表。
	/// </summary>
	public class CommentList : BaseRepeaterList
	{
		private LoadedValue<string> loadedInfoType;
		/// <summary>
		/// 检索评论的信息类别。
		/// </summary>
		public virtual string InfoType
		{
			get
			{
				if (loadedInfoType.Loader == null)
					loadedInfoType.Loader = () => (string)ViewState["InfoType"];
				return loadedInfoType.Value;
			}
			set
			{
				if (loadedInfoType.Setter == null)
					loadedInfoType.Setter = v => ViewState["InfoType"] = v;
				loadedInfoType.Value = value;
			}
		}

		private LoadedValue<string> loadedInfoId;
		/// <summary>
		/// 检索评论的信息标识。
		/// </summary>
		public virtual string InfoId
		{
			get
			{
				if (loadedInfoId.Loader == null)
					loadedInfoId.Loader = () => (string)ViewState["InfoId"];
				return loadedInfoId.Value;
			}
			set
			{
				if (loadedInfoId.Setter == null)
					loadedInfoId.Setter = v => ViewState["InfoId"] = v;
				loadedInfoId.Value = value;
			}
		}

		/// <summary>
		/// 评论更多列表Url.
		/// </summary>
		public override string ListMoreUrl
		{
			get
			{
				return GenericUrlManager.BuildCommentListUrl(InfoType, InfoId, null);
			}
		}

		/// <summary>
		/// 评论标题字段。
		/// </summary>
		public override string TitleField
		{
			get { return "Content"; }
		}

		/// <summary>
		/// 评论详细Url。（无效）
		/// </summary>
		public override string ItemUrl
		{
			get { return null; }
		}

		/// <summary>
		/// 评论时间字段。
		/// </summary>
		public override string DateTimeField
		{
			get { return "PostTime"; }
		}

		/// <summary>
		/// 评论标识。
		/// </summary>
		public virtual int ItemCommentId
		{
			get { return (int)Eval("CommentId"); }
		}

		/// <summary>
		/// 评论内容。
		/// </summary>
		public virtual string ItemContent
		{
			get { return Hxsoft.StringExtensions.HtmlEncode(Eval("Content", null)); }
		}

		/// <summary>
		/// 评论引用。
		/// </summary>
		public virtual string ItemQuote
		{
			get
			{
				string content = Eval("Quote", null);
				if (string.IsNullOrEmpty(content)) return content;
				return Hxsoft.RegexExtensions.ReplaceString(content, @"<div class=""quoteAuthor"">(.*?)</div>", string.Format(@"<div class=""quoteAuthor"">{0}</div>",Res.OriginalQuote));
			}
		}

		/// <summary>
		/// 评论者IP地址。
		/// </summary>
		public virtual string ItemPostIp
		{
			get { return Eval("PostIp", null); }
		}

		/// <summary>
		/// 评论者的用户名。匿名为空。
		/// </summary>
		public virtual string ItemUserName
		{
			get { return Eval("UserName", null); }
		}

		/// <summary>
		/// 显示的IP。
		/// </summary>
		public virtual string ItemDisplayPostIp
		{
			get
			{
				return "";
			}
		}

		/// <summary>
		/// 获取列表数据。
		/// </summary>
		/// <returns>更表数据。</returns>
		public override object GetListData()
		{
			return Comments.GetList(SiteId, InfoType, InfoId, LoadCount);
		}
	}
}